package com.ld.admin.webnotice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.webnotice.model.SocketIOClientInfo;
import com.ld.admin.webnotice.service.SocketIOService;
import com.ld.common.webnotice.socket.SocketIOServerUtil;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * socketio客户端信息
 * 
 * @ClassName SysUserCalendarController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年02月21日 14:32:08
 *
 */
@Api(tags = "socketio客户端信息")
@ConditionalOnProperty(prefix = "modules.sc.socketio", name = "admin.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "webnoticeSocketIOController")
@RequestMapping("${shieldsb.adminPath}/webnotice/socketio")
public class SocketIOController extends AdminBasicController<SocketIOClientInfo> {
    @Autowired
    private SocketIOService socketIOService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/webnotice/socketio/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/webnotice/socketio/socketio_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request, new QueryModel());
        socketIOService.getList(request, queryModel);
        return "a/webnotice/socketio/socketio_list";
    }

    @ApiOperation(value = "客户端信息展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        String id = getString("id");
        socketIOService.getOne(request, id);
        return "a/webnotice/socketio/socketio_show";
    }

    @ApiOperation(value = "到发送信息页面", notes = "")
    @RequestMapping(path = "/tosendmsg", method = RequestMethod.GET)
    public String toSendMsg() {
        setAttributes();
        return "a/webnotice/socketio/socketio_sendmsg";
    }

    @ApiOperation(value = "给客户端发送消息", notes = "")
    @RequestMapping(path = "/sendmsg", method = RequestMethod.POST)
    @ResponseBody
    public Object sendMsg2Client() {
        String id = getString("id");
        String message = getString("msg");
        String userId = Web.Util.getSessionUserId(request) + "";
        String userName = Web.Util.getSessionUser(request).getUserName();
        Result result = SocketIOServerUtil.sendMsg2Client(userId, userName, id, message);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "客户端断开连接", notes = "")
    @RequestMapping(path = "/disconn", method = RequestMethod.POST)
    @ResponseBody
    public Object disconn() {
        String id = getString("id");
        Result result = SocketIOServerUtil.disconnected(id);
        return Web.Response.result(result);
    }

}